package com.igroupes.ovineherd.model.Style;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class EnumStyle extends ChoiceListStyle {

    private Integer maxLength = 100;

    public EnumStyle(Integer maxLength, String label, Boolean required, JSONObject items, String desc) {
        this.maxLength = maxLength;
        this.label = label;
        this.required = required;
        this.items = items;
        this.desc = desc;
    }

    public EnumStyle(){
        super();
    }
    public EnumStyle(String label, JSONObject items, String desc) {
        super(label, false, items);
        this.desc = desc;
    }
    public EnumStyle(String label, Boolean required, JSONObject items) {
        super(label, required, items);
    }

    public EnumStyle(String label, List value, JSONObject controls, String desc) {
        super(label, controls, desc);
        this.value = value;
        this.required = false;
    }
    public EnumStyle(String label, List value, JSONObject controls, String desc, Boolean required) {
        super(label, controls, desc);
        this.value = value;
        this.required = required;
    }

    @Override
    public String getType() {
        return FormKey.ENUM.getCode();
    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof EnumStyle)){
            throw new ModelException("校验style类类型错误，enum");
        }
        EnumStyle style = (EnumStyle) o;
        if(style.getItems().size() > 0){
            super.verify(o);
            if(ObjectUtil.isNotNull(style.getValue())){
                for (Object g :
                        style.getValue()) {
                    if(!(g instanceof TextStyle)){
                        throw new ModelException(style.getLabel()+"传值类型错误");
                    }
                    TextStyle group1 = (TextStyle) g;
                    group1.verify(g);
                }
            }
        }
        return true;
    }
    /**
     * 获得所有选中值
     * @param o
     * @return
     */
    public static List<String> fields(Object o){
        List<String> stringList = new ArrayList<>();
        EnumStyle style = (EnumStyle) o;
//        if()
//        if(style.getItems().size() > 0){
//            for (Object g :
//                    style.getValue()) {
//                TextStyle group1 = (TextStyle) g;
//                stringList.add(Convert.toStr(group1.getValue()));
//            }
//        }
        return stringList;
    }
}
